package lincen.javame.game.handheldgame.tetris;


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

import lincen.javame.game.handhelddevice.HandHeldCanvas;
import lincen.javame.game.tetris.Matrix;
import lincen.javame.light.util.Tool;

/**
 * 俄罗斯方块程序入口
 * @author 林岑
 */
public class GameFrame extends JFrame implements Runnable, KeyListener { // NO_UCD

	/**
	 * 俄罗斯方块核心控制器
	 */
	private Matrix mx;

	/**
	 * 点阵画布
	 */
	private TetrisCanvas tc;

	/**
	 * 运行标记
	 */
	private boolean running;

	/**
	 * 方块下落一格所用的时间单位时间
	 */
	private float daltaT;

	/**
	 * 是否快速下沉
	 */
	private boolean isFastDown;

	/**
	 * 下次升级所需分数
	 */
	private int nextLevelScoreShould;

	/**
	 * 加分策略
	 */
	private AddScoreStratege addScoreStratege = new AddScoreStratege();

	private byte temp;

	public GameFrame() {
		this.setLocation(200, 200);
		this.setSize(220, 200);
	}
	
	/**
	 * 运行
	 */
	public void run() {
		boolean isFallToTheTroundOrStack;
		long T1 = System.currentTimeMillis();
		long T2 = T1;
		while (isRunning()) {
			if (T2 - T1 > daltaT || isFastDown) {
				isFallToTheTroundOrStack = this.mx.down();

				// 如果在下沉过程中, 落到地上或者堆上
				// 就清除mx中的满行
				if (isFallToTheTroundOrStack) {
					int lineNum = this.mx.clearFullLines();
					this.tc.addScore(this.addScoreStratege.getScore(lineNum, this.mx));
					adjustLevel();
				}

				// 如果mx中的堆满了. 那么就执行死亡方法
				if (this.mx.isStackFull()) {
					death();
				}
				T1 = T2;
			}
			input();
			paint();
			T2 = System.currentTimeMillis();
			Tool.sleepDaltaT(20);
		}
	}




	public static void main(String[] args) {

		GameFrame r = new GameFrame();
//
//		//如果是刚刚启动程序, 就要进行初始化
//		if(r.tc == null) {
//			r.init();
//		}
//		r.start();
	}

	protected void pauseApp() {
		stop();
	}

	/**
	 * 初始化各个组件
	 */
	private void init() {
		this.tc = null;
		this.mx = null;
		this.tc = new TetrisCanvas();
		this.mx = new Matrix(tc.getRow(), tc.getCol());

		this.isFastDown = false;
		this.nextLevelScoreShould = 300;
		this.temp = 0;
		this.running = false;
		adjustSpeed();
	}

	/**
	 * 重来
	 */
	private void restart() {
		this.init();
		this.start();
	}

	/**
	 * 死亡
	 */
	private void death() {
		stop();
	}

	/**
	 * 达到一定条件就升级
	 */
	private void adjustLevel() {
		if(this.tc.getLevel() < 7 && this.tc.getScore() >= this.nextLevelScoreShould) {
			levelUp();
		}
	}

	/**
	 * 升级
	 */
	private void levelUp() {
		this.tc.setLevel(this.tc.getLevel() + 1);
		this.nextLevelScoreShould = this.addScoreStratege.getNextLevelScoreShould(this.tc.getLevel());
		adjustSpeed();
	}

	/**
	 * 输入
	 */
	private void input() {
//		int ks = this.tc.getKeyStates();
//		if((ks & GameCanvas.LEFT_PRESSED) != 0) {
//			if(temp++ > 5) {
//				this.mx.left();
//				temp = 6;
//			}
//		} else if ((ks & GameCanvas.RIGHT_PRESSED) != 0) {
//			if(temp++ > 5) {
//				this.mx.right();
//				temp = 6;
//			}
//		} else {
//			this.temp = 0;
//		}
	}

	/**
	 * 将mx绘制到hhc中
	 */
	private void paint() {
		this.tc.setPointMain(this.mx.getPointGraphics());
		this.tc.setPointNext(this.mx.getPointNext());
		this.tc.paintToBuffered();
	}

	/**
	 * 判断程序是否正在运行
	 * 
	 * @return
	 */
	private boolean isRunning() {
		return this.running;
	}

	/**
	 * 设置方块下落速度
	 * 
	 * @param speed
	 */
	private void setSpeed(float speed) {
		this.daltaT = 1000f / speed;
	}

	/**
	 * 根据等级设置下降速度
	 */
	private void adjustSpeed() {
		switch (this.tc.getLevel()) {
		case 0:
			setSpeed(3);
			break;

		case 1:
			setSpeed(4);
			break;

		case 2:
			setSpeed(5);
			break;

		case 3:
			setSpeed(6);
			break;

		case 4:
			setSpeed(7);
			break;

		case 5:
			setSpeed(10);
			break;

		case 6:
			setSpeed(16);
			break;

		default:
			setSpeed(20);
			break;
		}
	}

	/**
	 * 开始游戏
	 */
	private void start() {
		if (!isRunning()) {
			this.running = true;
			new Thread(this).start();
		}
	}

	/**
	 * 停止运行
	 */
	private void stop() {
		this.running = false;
	}


	/**
	 * 俄罗斯方块画布
	 * 
	 * @author 林岑
	 */
	private class TetrisCanvas extends HandHeldCanvas {

		/**
		 * 加分
		 * @param score
		 */
		public void addScore(int score) {
			this.setScore(this.getScore() + score);
		}
	}


	@Override
	public void keyPressed(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_UP:
			mx.turnLeft();
			break;
		case KeyEvent.VK_DOWN:
			isFastDown = true;
			break;
		case KeyEvent.VK_LEFT:
			mx.left();
			break;
		case KeyEvent.VK_RIGHT:
			mx.right();
			break;
		case KeyEvent.VK_0:
			restart();
			break;
		default:
			if(isRunning()) {
				stop();
			} else {
				start();
			}
			break;
		}
	}




	@Override
	public void keyReleased(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_DOWN:
			isFastDown = false;
		default:
			break;
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}
}
